//
//  YHEnemyShip.cpp
//  ThunderWarriors
//
//  Created by mac on 13-6-23.
//
//

#include "YHEnemyShip.h"
#include "YHGameDefine.h"
#include "YHBullet.h"
#include "YHEffect.h"
#include "YHGameLayerFight.h"

YHEnemyShip::YHEnemyShip(){
    m_enemyType.scoreValue = 100;
    m_enemyType.bulletPowerValue = 1;
    m_enemyType.bulletSpeed  = 100;
    m_enemyType.enemyTextureName = "E0.png";
    m_enemyType.hp = 5;
    m_enemyType.velocityx = 0;
    m_enemyType.velocityy = 100;
    m_enemyType.actionType = YHEnemyActionType0;
    m_isActive = true; 
}

YHEnemyShip::~ YHEnemyShip(){
    
}

bool YHEnemyShip::initWithEnemyType(YHEnemyType enemyType){
    m_enemyType = enemyType;
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(s_Enemy_plist, s_enemys);
    bool ret = this->initWithSpriteFrameName(m_enemyType.enemyTextureName.c_str());
    
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(s_bullet_plist);

    this->schedule(schedule_selector(YHEnemyShip::shoot), 2);
    
    return ret;
}

YHEnemyShip* YHEnemyShip::createWithEnemyType(YHEnemyType enemyType){
    YHEnemyShip* pRet = new YHEnemyShip();
    if (pRet && pRet->initWithEnemyType(enemyType)) {
        pRet->autorelease();
        return pRet;
    }
    if (pRet) delete pRet;
    return NULL;
}

void YHEnemyShip::destroy(){
    YHEffect* effect = YHEffect::create();
    effect->explode(this->getParent(), this->getPosition());
    this->removeFromParentAndCleanup(true);
}

void YHEnemyShip::hurt(){
    m_enemyType.hp --;
    if (m_enemyType.hp <= 0) {
        m_isActive = false;
    }
}

CCRect YHEnemyShip::getCollideCrect(){
    CCSize size = getContentSize();
    CCPoint pos = this->getPosition();
    
    return CCRectMake(pos.x - size.width/2, pos.y - size.height/2, size.width, size.height);
}

bool YHEnemyShip::init(){
    return CCSprite::init();
}


void YHEnemyShip::udpate(float dt){
    
}

void YHEnemyShip::shoot(float t){
    if (this->getParent() == NULL) {
        return;
    }
    else{
        CCSpriteBatchNode* enemyBullets = YHGameLayerFight::sharedGameLayerFight()->getEnemyBullets();
        YHBullet* bullet = YHBullet::create(s_enemy_bullet, kEnemyBulletSpeed);
        CCPoint pos = this->getPosition();
        pos.y = pos.y - getContentSize().height*0.3;
        bullet->setPosition(pos);
        enemyBullets->addChild(bullet);
    }
}

